/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.maze;

import java.util.ArrayList;
import org.maze.display.IDisplay;
import org.maze.Population.Population;
import org.maze.configuration.PopulationConfiguration;
import org.maze.configuration.TestConfiguration;
import org.maze.statistic.Statistic;

/**
 *
 * @author tpasquie
 */
public class Main {
    static TestConfiguration mainConf=null;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception{
        try{
            if(args.length<1){
                mainConf = new TestConfiguration("./config/best.xml");
            }else{
                mainConf = new TestConfiguration(args[0]);
            }
            ArrayList<PopulationConfiguration> popConfigs = mainConf.getConfigurations();
            //IDisplay disp = new ConsoleDisplay();
            IDisplay disp = mainConf.getDisplay();
            // run 4 test
            for(int i=0; i < 4; i++){
                // generate a different maze for each test
                Maze maze = Maze.getInstance();
                maze.randomGenerate();

                // go through each pop in the test config
                for(PopulationConfiguration pc : popConfigs){
                    Population p = new Population(pc.getIndividualConf(), pc.getPopulationSize(), pc.getNbVillages());
                    Statistic stat = new Statistic("./results/"+pc.getName()+"_"+i+".csv");
                    do{
                        p.nextGeneration();
                        disp.setGeneration(p.getGeneration());
                        disp.setPath(p.getFitest().getPath());
                        disp.setFitest(p.getFitest());
                        disp.setAverage(p.getAverageScore());
                        disp.setDiversity(p.getAverageDiversity());
                        disp.refresh();
                        if(p.getGeneration()%5==1)
                            stat.addLine(p);
                    }while(p.getGeneration() < 500 && p.getAverageDiversity()>0.0005);
                }
            }
        }catch(Exception e){
            System.out.println("Exception : "+e.getMessage());
            throw e;
        }
    }

}
